package com.riaya.controller;

import com.github.pagehelper.PageInfo;
import com.riaya.entity.Squad;
import com.riaya.service.SquadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/squad")
@CrossOrigin
public class SquadController {

    @Autowired
    private SquadService squadService;

    /**
     * 查询所有队伍信息
     *
     * @return
     */
    @RequestMapping("/list")
    public Object selectList(@RequestParam(required = false, defaultValue = "1") Integer pageNo, @RequestParam(required = false, defaultValue = "5") Integer pageSize, @RequestParam(required = false) String name) {
        PageInfo<Squad> allSquad = squadService.getAllSquad(pageNo, pageSize, name);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", "20000");
        map.put("msg", "成功");
        map.put("data", allSquad);
        return map;
    }

    /**
     * 根据id查询队伍信息
     *
     * @param id
     * @return
     */
    @RequestMapping("/list/{id}")
    public Object selectSquadId(@PathVariable Integer id, Model model) {
        Squad squadId = squadService.querySquadId(id);
        model.addAttribute("squadId", squadId);
        //以下我也没试过,我感觉铁定报错
        Map<String,Object> map = new HashMap<String,Object>();
        if(squadId != null) {
            map.put("code", "20000");
            map.put("msg","查询成功");
            map.put("data", model);
        }else{
            map.put("code", "50000");
            map.put("msg","查询失败");
        }

        return map;
    }

    /**
     * 添加队伍信息
     *
     * @param squad
     */
    @PostMapping("/add")
    public Map<String, Object> addSquad(@RequestBody @Valid Squad squad) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            int rows = squadService.addSquad(squad);
            if (rows > 0) {
                result.put("code", "20888");
                result.put("msg", "添加成功");
                result.put("data", squad.getId()); // 假设返回生成的主键
            } else {
                result.put("code", "50001");
                result.put("msg", "添加失败");
            }
        } catch (Exception e) {
            result.put("code", "50002");
            result.put("msg", "服务器异常: " + e.getMessage());
        }
        return result;
    }
    /**
     * 修改队伍信息
     *
     * @param squad
     */
    @PostMapping("/updateSquad")
    public void updateSquad(Squad squad) {
        squadService.updateSquad(squad);
        System.out.println("修改队伍信息成功");
        System.out.println(squad);
    }



    /**
     * 删除队伍信息
     *
     * @param id
     */
   @GetMapping("/deleteSquad/{id}")
    public String deleteSquad(@PathVariable Integer id){
       if(squadService.delSquad(id)){
           return "删除成功";
       }else {
           return "删除失败";
       }
   }




}

